package com.hermit.qmst;

import android.app.Application;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Environment;
import android.os.Handler;
import android.provider.ContactsContract;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;

import com.hermit.qmst.mode.ContactInfo;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;
import com.hermit.qmst.tools.Utils;

import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by hermithermit on 15/2/12.
 * Application 的生命周期是整个软件的生命周期
 */
public class MyApplication extends Application{

    public ArrayList<ContactInfo> mContactInfoList;
    public static MyApplication mInstance;
    private Handler mHandler;
    private AsyncQueryHandler mAsyncQueryHandler;
    public View mCallPhoneWidget;


    public static final String IN_COMING_CALLING_ACTION = "com.common.in.coming.calling.action";

    //call bar tag
    public static final String DIAL_CALL_BAR_TAG = "DialCallBar";
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        mInstance = this;
        mHandler = new Handler() ;
        mContactInfoList = new ArrayList<ContactInfo>();
        mAsyncQueryHandler = new MyAsyncQueryHanlder(getContentResolver());
        startQueryContactInfos(80);

        DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
        Common.mHeightPixels = displayMetrics.heightPixels;
        Common.mWidthPixels = displayMetrics.widthPixels;
        //遮挡条
        mCallPhoneWidget = (View) LayoutInflater.from(this).inflate(R.layout.widget_callphone,null);


        if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED){
            Common.mRootDirPath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/szdh";
        }else if(getExternalFilesDir(null) != null){


            Common.mRootDirPath =  getExternalFilesDir(null).getAbsolutePath();

        }else {
            Common.mRootDirPath = getFilesDir().getAbsolutePath();
        }

    }



    public static MyApplication getInstance(){
        return mInstance;
    }

    //开始查询
    private void startQueryContactInfos(int delay) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                String projection[] = {ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.DATA1, ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY,
                        ContactsContract.CommonDataKinds.Phone.PHOTO_ID, ContactsContract.CommonDataKinds.Phone.CONTACT_ID};

                mAsyncQueryHandler.startQuery(0, null, ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, "sort_key COLLATE LOCALIZED asc");
            }
        },delay);
    }

    private class MyAsyncQueryHanlder extends AsyncQueryHandler{

        private MyAsyncQueryHanlder(ContentResolver cr) {
            super(cr);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            super.onQueryComplete(token, cookie, cursor);

            if (cursor != null && cursor.getCount() > 0){
                mContactInfoList.clear();
                for (int i = 0; i < cursor.getCount(); i++){
                    cursor.moveToPosition(i);
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    if (name == null){
                        name = "";
                    }

                    String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA1));
                    if (phone == null || phone.length() < 11){
                        continue;
                    }

                    phone = Utils.analysePhoneNumber(phone);

                    ContactInfo info = new ContactInfo();
                    info.setName(name);
                    info.setPhone1(phone);
                    info.setPinYin(Utils.getPinYin(name));

                    mContactInfoList.add(info);
                }
                cursor.close();

                Log.i("MyApplication","mContactInfoList size = " + mContactInfoList.size(),true);
            }

        }
    }

}
